using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;

namespace BAS.AsTweet {

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Settings class manages Module Settings
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class Settings : ModuleSettingsBase {

        #region Base Method Implementations

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadSettings loads the settings from the Database and displays them
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void LoadSettings() {
            try {
                if (!IsPostBack) {
                    tbTweetCount.Text = Settings.ContainsKey(Constants.KEY_ITEMS_COUNT) ? Settings[Constants.KEY_ITEMS_COUNT].ToString() : 10.ToString();
                    tbOptionalScreen.Text = Settings.ContainsKey(Constants.KEY_OPTIONAL_SCREEN) ? Settings[Constants.KEY_OPTIONAL_SCREEN].ToString() : "";
                }
            } catch (Exception ex) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// UpdateSettings saves the modified settings to the Database
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void UpdateSettings() {
            try {
                ModuleController modules = new ModuleController();
                modules.UpdateTabModuleSetting(this.TabModuleId, Constants.KEY_ITEMS_COUNT, tbTweetCount.Text);
                modules.UpdateModuleSetting(this.TabModuleId, Constants.KEY_OPTIONAL_SCREEN, tbOptionalScreen.Text);
            } catch (Exception ex) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        #endregion

    }

}

